﻿using kevin.zhang.canada.at.gmail.com;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml.Linq;
using System.Collections.Generic;

namespace TestProject1
{
    
    
    /// <summary>
    ///This is a test class for DeltaTest and is intended
    ///to contain all DeltaTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DeltaTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public class TestStrings
        {
            public string caseName { get; set; }
            public string source { get; set; }
            public string target { get; set; }
        }

        public class AllTestCases
        {
            public List<TestStrings> testStringCollection = new List<TestStrings>();
            public AllTestCases()
            {
                TestStrings testString;
                testString = new TestStrings
                {
                    caseName = "test all changes",
                    source = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl",
                    target = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9"
                };
                testStringCollection.Add(testString);

                testString = new TestStrings
                {
                    caseName = "test all changes in one line",
                    source = "a b c d e f g h i j k l",
                    target = "0 1 2 3 4 5 6 7 8 9"
                };
                testStringCollection.Add(testString);

                testString = new TestStrings
                {
                    caseName = "test all same",
                    source = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl",
                    target = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl"
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "test all same in one line",
                    source = "a b c d e f g h i j k l",
                    target = "a b c d e f g h i j k l"
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "test snake",
                    source = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl",
                    target = "b\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nx"
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "",
                    source = "",
                    target = ""
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "20020920 - repro",
                    source = "c1,a,c2,b,c,d,e,g,h,i,j,c3,k,l".Replace(',', '\n'),
                    target = "C1,a,C2,b,c,d,e,I1,e,g,h,i,j,C3,k,I2,l".Replace(',', '\n')
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "2003.02.07 - repro",
                    source = "F",
                    target = "0,F,1,2,3,4,5,6,7".Replace(',', '\n')
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "Muegel - repro",
                    source = "HELLO\nWORLD",
                    target = "\n\nhello\n\n\n\nworld\n"
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "test some differences",
                    source = "a,b,-,c,d,e,f,f".Replace(',', '\n'),
                    target = "a,b,x,c,e,f".Replace(',', '\n')
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "test one change within long chain of repeats",
                    source = "a,a,a,a,a,a,a,a,a,a".Replace(',', '\n'),
                    target = "a,a,a,a,-,a,a,a,a,a".Replace(',', '\n')
                };
                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "mixed lines and inline",
                    source = @"O Canada,
Our home and native land, 
True patriot love 
In all thy sons command. 
With glowing hearts 
We see thee rise, 
The true, north, strong, and free. 
From far and wide 
Oh Canada 
We stand on guard for thee. 
God keep our land 
Glorious and free. 
Oh, Canada we stand on guard for thee, 
Oh, Canada 
we stand on guard for thee",
                    target = @"O Canada!
Terre de nos aîeux, 
Ton front est ceint 
de fleurons glorieux! 
Car ton bras 
sait porter l'épée, 
Il sait porter 
la croix! Ton histoire 
est une épopée, 
Des plus brillants exploits. 
Et ta valeur, 
de foi trempée, 
Protégera nos foyers et nos droits. 
Protégera nos foyers 
et nos droits."
                };
                testStringCollection.Add(testString);

                testStringCollection.Add(testString);
                testString = new TestStrings
                {
                    caseName = "mixed lines and inline2",
                    source = @"Oh, Canada 
Our home and native land, 
True patriot love 
In all thy sons command. 
With glowing hearts 
We see thee rise, 
The true, north, strong, and free. 
From far and wide 
Oh Canada 
We stand on guard for thee. 
God keep our land 
Glorious and free. 
Oh, Canada we stand on guard for thee, 
Oh, Canada 
we stand on guard for thee",
                    target = @"Oh, CANADA 
Our home and great land, 
True patriot love 
In all thy sons and daughters command. 
With glowing hearts 
We see thee rise, 
The true and free. 
From far and wide 
Oh CAD 
We stand here I added some words to test on guard for thee. 
God keep our land 
here I add one line to test
Oh, Canada we stand on guard for thee, 
Oh, Canada 
we stand on guard for thee
here I insert last line to test
I love my country, how about you?"
                };
                testStringCollection.Add(testString);
            }
        }

        
        /// <summary>
        ///A test for GetChangeSet
        ///</summary>
        [TestMethod()]
        public void GetChangeSetTest()
        {
            var testcases = new AllTestCases();
            foreach (var item in testcases.testStringCollection)
            {
                string source = item.source;
                string target = item.target;
                Delta.ChangeSet actual;
                actual = Delta.GetChangeSet(source, target);
                string afterTransform = Delta.ApplyChangeSet(source, actual);
                Assert.AreEqual(afterTransform, target);
            }
        }
    }
}
